package com.book.lizhi.read;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.book.lizhi.R;
import com.book.lizhi.constants.Constants;
import com.book.lizhi.ui.ChapterActivity;
import com.book.lizhi.util.Util;

public class ReadMenu {
	private Handler mHandler;
	private Context mContext;

	private View mAnchorView;
	private View mTopMenu;
	private PopupWindow mBoottomMenu;
	private PopupWindow mSettingWindow;
	private PopupWindow mRateWindow;

	private boolean isSystem;
	private int index;

	private View[] mBackgroudView;
	private TextView showRateView;
	private SeekBar rateProgressBar;

	public int defaultMode = Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;

	public ReadMenu(Context context, Handler handler, View topMenu, View anchorView) {
		mContext = context;
		mHandler = handler;
		mTopMenu = topMenu;
		mAnchorView = anchorView;
		mBackgroudView = new View[4];
		initUI();
	}

	private void initUI() {
		Resources resources = mContext.getResources();

		View bottomView = View.inflate(mContext, R.layout.read_bottom_layout, null);
		mBoottomMenu = new PopupWindow(bottomView);
		mBoottomMenu.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		mBoottomMenu.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		mBoottomMenu.setBackgroundDrawable(new BitmapDrawable(resources, BitmapFactory.decodeResource(resources, R.drawable.read_menu_backgroud)));
		initBottomMenu(bottomView);

		View rateView = View.inflate(mContext, R.layout.read_rate_layout, null);
		mRateWindow = new PopupWindow(rateView);
		mRateWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		mRateWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		mRateWindow.setBackgroundDrawable(new BitmapDrawable(resources, BitmapFactory.decodeResource(resources, R.drawable.read_menu_backgroud)));
		initRateView(rateView);

		View settingView = View.inflate(mContext, R.layout.read_settting_layout, null);
		mSettingWindow = new PopupWindow(settingView);
		mSettingWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		mSettingWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		mSettingWindow.setBackgroundDrawable(new BitmapDrawable(resources, BitmapFactory.decodeResource(resources, R.drawable.read_menu_backgroud)));
		initSettingView(settingView);
	}

	private void initRateView(View rateView) {
		rateView.findViewById(R.id.rate_pre_view).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mHandler.sendEmptyMessage(ReadActivity.READ_PRE_CHAPTER);
			}
		});
		showRateView = (TextView) rateView.findViewById(R.id.rate_rate_view);
		rateProgressBar = (SeekBar) rateView.findViewById(R.id.rate_seekbar);
		rateProgressBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				if (fromUser) {
					mHandler.obtainMessage(ReadActivity.READ_NEXT_CHAPTER, progress, 0).sendToTarget();
				}
			}
		});
		rateView.findViewById(R.id.rate_next_view).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mHandler.sendEmptyMessage(ReadActivity.READ_NEXT_CHAPTER);
			}
		});
	}

	public void setRate(int max, int progress, String text) {

		showRateView.setText(text);
		rateProgressBar.setMax(max);
		rateProgressBar.setProgress(progress);
	}

	private void initSettingView(View settingView) {
		mSettingWindow.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				Util.saveDataBySharePreference(Constants.READ_BACKGROUD, index);
			}
		});
		settingView.findViewById(R.id.text_small_setting).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mHandler.obtainMessage(ReadActivity.CHANGE_TEXT_SIZE, -1, 0).sendToTarget();
			}
		});
		settingView.findViewById(R.id.text_big_settting).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mHandler.obtainMessage(ReadActivity.CHANGE_TEXT_SIZE, 1, 0).sendToTarget();
			}
		});

		final int light = getSystemLight(settingView.getContext());
		final SeekBar lightSeekBar = (SeekBar) settingView.findViewById(R.id.light_seekbar);
		TextView systemView = (TextView) settingView.findViewById(R.id.system_light_view);
		lightSeekBar.setMax(255);
		isSystem = Util.getBooleanBySharePreference(Constants.READ_LIGHT_SYSTEM);
		if (isSystem) {
			systemView.setTextColor(settingView.getResources().getColor(R.color.green_14be7d));
			lightSeekBar.setEnabled(false);
		} else {
			systemView.setTextColor(settingView.getResources().getColor(R.color.white));
			lightSeekBar.setEnabled(true);
			initBrightness(settingView.getContext());
			if (Util.getIntBySharePreference(Constants.READ_LIGHT_COUNT) == 0) {
				setScreenMode(settingView.getContext(), light);
				lightSeekBar.setProgress(light);
			}
		}
		settingView.findViewById(R.id.system_light_view).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isSystem) {
					setScreenMode(v.getContext(), Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
					lightSeekBar.setProgress(light);
					lightSeekBar.setEnabled(true);
					((TextView) v).setTextColor(v.getResources().getColor(R.color.green_14be7d));
					isSystem=false;
				} else {
					setScreenMode(v.getContext(), Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
					lightSeekBar.setEnabled(false);
					isSystem = true;
					((TextView) v).setTextColor(v.getResources().getColor(R.color.white));
				}
				Util.saveDataBySharePreference(Constants.READ_LIGHT_SYSTEM, isSystem);
			}
		});

		lightSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				Util.saveDataBySharePreference(Constants.READ_LIGHT_COUNT, seekBar.getProgress());
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				setScreenMode(seekBar.getContext(), progress);
			}
		});

		mBackgroudView[0] = settingView.findViewById(R.id.them_0_view);
		mBackgroudView[0].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				v.setBackgroundResource(R.drawable.p_bg_menu_theme_box);
				mBackgroudView[index].setBackgroundResource(0);
				index = 0;
				v.setBackgroundResource(R.drawable.p_bg_menu_theme_box);
				mHandler.obtainMessage(ReadActivity.CHAGE_BACKGROUD, v.getResources().getColor(R.color.backgroud_0), 0).sendToTarget();
			}
		});
		mBackgroudView[1] = settingView.findViewById(R.id.them_1_view);
		mBackgroudView[1].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				v.setBackgroundResource(R.drawable.p_bg_menu_theme_box);
				mBackgroudView[index].setBackgroundResource(0);
				index = 1;
				mHandler.obtainMessage(ReadActivity.CHAGE_BACKGROUD, v.getResources().getColor(R.color.backgroud_1), 1).sendToTarget();
			}
		});
		mBackgroudView[2] = settingView.findViewById(R.id.them_2_view);
		mBackgroudView[2].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				v.setBackgroundResource(R.drawable.p_bg_menu_theme_box);
				mBackgroudView[index].setBackgroundResource(0);
				index = 2;
				mHandler.obtainMessage(ReadActivity.CHAGE_BACKGROUD, v.getResources().getColor(R.color.backgroud_2), 2).sendToTarget();
			}
		});
		mBackgroudView[3] = settingView.findViewById(R.id.them_3_view);
		mBackgroudView[3].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				v.setBackgroundResource(R.drawable.p_bg_menu_theme_box);
				mBackgroudView[index].setBackgroundResource(0);
				index = 3;
				mHandler.obtainMessage(ReadActivity.CHAGE_BACKGROUD, v.getResources().getColor(R.color.backgroud_3), 3).sendToTarget();
			}
		});
		index = Util.getIntBySharePreference(Constants.READ_BACKGROUD);
		mBackgroudView[index].setBackgroundResource(R.drawable.p_bg_menu_theme_box);

	}

	private void initBrightness(Context context) {

		try {
			defaultMode = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
			// 如果当前的屏幕亮度调节调节模式为自动调节，则改为手动调节屏幕亮度
			if (defaultMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
				setScreenMode(context, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
			}
		} catch (SettingNotFoundException e) {
			e.printStackTrace();
		}
	}

	private void setScreenMode(Context context, int value) {
		Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, value);
	}

	private int getSystemLight(Context context) {
		try {
			return Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
		} catch (SettingNotFoundException e) {
			e.printStackTrace();
		}
		return 0;
	}

	private void initBottomMenu(View bottomView) {

		// 文字设置
		bottomView.findViewById(R.id.menu_text_view).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
				if (mSettingWindow.isShowing()) {
					mSettingWindow.dismiss();
				} else {
					mSettingWindow.showAtLocation(mAnchorView, Gravity.BOTTOM, 0, 0);

				}
			}
		});
		bottomView.findViewById(R.id.menu_progress_view).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
				if (mRateWindow.isShowing()) {
					mRateWindow.dismiss();
				} else {
					mRateWindow.showAtLocation(mAnchorView, Gravity.BOTTOM, 0, 0);
				}
			}
		});
		ImageView menuTimeView = (ImageView) bottomView.findViewById(R.id.menu_time);
		if (Util.getBooleanBySharePreference(Constants.READ_MODE)) {
			menuTimeView.setImageResource(R.drawable.icon_menu_bottom3_night);
		} else {
			menuTimeView.setImageResource(R.drawable.icon_menu_bottom3_light);
		}
		menuTimeView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
				boolean isDay = Util.getBooleanBySharePreference(Constants.READ_MODE);
				if (!isDay) {
					int index = Util.getIntBySharePreference(Constants.READ_BACKGROUD);
					int backgroud = 0;
					switch (index) {
					case 0:
						backgroud = v.getResources().getColor(R.color.backgroud_0);
						break;
					case 1:
						backgroud = v.getResources().getColor(R.color.backgroud_1);

						break;
					case 2:
						backgroud = v.getResources().getColor(R.color.backgroud_2);

						break;
					case 3:
						backgroud = v.getResources().getColor(R.color.backgroud_3);

						break;

					default:
						break;
					}
					Util.saveDataBySharePreference(Constants.READ_MODE, true);
					mHandler.obtainMessage(ReadActivity.CHAGE_BACKGROUD, backgroud, 0).sendToTarget();
					((ImageView) v).setImageResource(R.drawable.icon_menu_bottom3_night);
				} else {
					Util.saveDataBySharePreference(Constants.READ_MODE, false);
					mHandler.obtainMessage(ReadActivity.CHAGE_BACKGROUD, v.getResources().getColor(R.color.backgroud_4), 0).sendToTarget();
					((ImageView) v).setImageResource(R.drawable.icon_menu_bottom3_light);
				}
			}
		});
		bottomView.findViewById(R.id.menu_chapter_view).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
				v.getContext().startActivity(new Intent(v.getContext(), ChapterActivity.class));
			}
		});

	};

	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_MENU) {

			if (mRateWindow.isShowing()) {
				mRateWindow.dismiss();
			}

			if (mSettingWindow.isShowing()) {
				mSettingWindow.dismiss();
			}

			if (mBoottomMenu.isShowing()) {
				mBoottomMenu.dismiss();
				mTopMenu.setVisibility(View.GONE);
			} else {
				mBoottomMenu.showAtLocation(mAnchorView, Gravity.BOTTOM, 0, 0);
				mTopMenu.setVisibility(View.VISIBLE);
				mTopMenu.bringToFront();
			}
			return true;

		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (mRateWindow.isShowing()) {
				mRateWindow.dismiss();
				return true;
			}
			if (mSettingWindow.isShowing()) {
				mSettingWindow.dismiss();
				return true;
			}

			if (mBoottomMenu.isShowing()) {
				mBoottomMenu.dismiss();
				mTopMenu.setVisibility(View.GONE);
				return true;
			}
		}

		return false;
	}

	public void dismiss() {
		onKeyDown(KeyEvent.KEYCODE_BACK, null);
	}

	public void restDefaultSetting() {
		setScreenMode(mContext, defaultMode);
	}

}
